package service.impl;

import dao.DesignClassCourseMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.ClassCourseM;
import service.DesignClassCourseService;

import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class DesignClassCourseServiceImpl implements DesignClassCourseService {
    @Autowired
    DesignClassCourseMapper designClassCourseMapper;

    @Override
    public List<ClassCourseM> getClassCourse() {

        return designClassCourseMapper.getClassCourse();
    }

    @Override
    public List<ClassCourseM> getOne(Integer grade) {
        return designClassCourseMapper.getOne(grade);
    }

    @Override
    public int DesignClassCourse(ClassCourseM classCourseM) {
        log.info("classCourseM="+classCourseM);
        if(classCourseM.getId() == null){
            log.warn("classCourseM缺少id,随机生成中");
            classCourseM.setId(UUID.randomUUID().toString());
        }
        return designClassCourseMapper.insert(classCourseM);
    }

    @Override
    public List<ClassCourseM> getClassCourseByArray(Integer currPage, Integer pageSize) {
        List<ClassCourseM> classCourseMS = designClassCourseMapper.getClassCourse();
        int firstIndex = (currPage -1 )*pageSize;
        int lastIndex = currPage * pageSize;
        if((classCourseMS.size()-firstIndex)<pageSize){
            classCourseMS =classCourseMS.subList(firstIndex,classCourseMS.size());
        }
        try {
            classCourseMS = classCourseMS.subList(firstIndex, lastIndex);
        }catch (Exception e){
            e.printStackTrace();
        }
        return classCourseMS;
    }

    @Override
    public Integer delete(String id) {
        return designClassCourseMapper.delete(id);
    }
}
